// import qs from 'qs'
import axios from 'axios'
import { notification as Notification, message } from 'ant-design-vue'
const instance = axios.create({
  timeout: 10000
})

instance.interceptors.request.use(
  (config) => {
    const method = config.method.toUpperCase()
    if (config.method === 'get') {
      const getParams = config.params
      console.log(
        `%c ${method} 请求  ${config.url}`,
        'color:#f5222d',
        getParams !== '' ? getParams : ''
      )
    } else if (config.method === 'post') {
      const postData = config.data
      console.log(`%c ${method} 请求  ${config.url}`, 'color:#1890ff', postData !== '' ? postData : '')
    } else {
      console.log(`%c ${method} 请求  ${config.url}`, 'color:#eb2f96', config.data ? config.data : '')
    }
    return config
  }
)

instance.interceptors.response.use(
  (response) => {
    console.log(
        `%c 返回  ${response.config.url}  --`,
        'color:#19be6b',
        response.data
    )
    const { errCode } = response.data
    if (errCode * 1 !== 0) {
      message.error(`${errCode},${response.data.message}`)
    }
    return response.data
  },
  (error) => {
    // 对响应错误做点什么
    console.log('error--------------', error)
    if (error && error.response && error.response.status) {
      const codeMaps = {
        400: '400,错误请求',
        401: '401,未授权，请重新登录',
        403: '403,拒绝访问',
        404: '404,请求错误,未找到该资源',
        405: '405,请求方法未允许',
        408: '408,请求超时',
        500: '500,服务器端出错',
        501: '501,网络未实现',
        502: '502,网络错误',
        503: '503,服务不可用',
        504: '504,网络超时',
        505: '505,http版本不支持该请求'
      }
      error.message = codeMaps[error.response.status] || `连接错误${error.response.status}`
    } else {
      error.message = '连接到服务器失败,服务未开启'
    }
    Notification.error({
      message: error.message
    })
    return Promise.reject(error.message)
  }
)

export const $get = (url, params) => {
  return new Promise((resolve, reject) => {
    instance({
      method: 'get',
      url: url,
      params: params
    }).then(res => {
      resolve(res)
    }).catch(error => {
      reject(error)
    })
  })
}
export const $post = (url, data) => {
  return new Promise((resolve, reject) => {
    instance({
      method: 'post',
      url: url,
      data: data
    }).then(res => {
      resolve(res)
    }).catch(error => {
      reject(error)
    })
  })
}

export const $delete = (url) => {
  return new Promise((resolve, reject) => {
    instance({
      method: 'delete',
      url: url
    }).then(res => {
      resolve(res)
    }).catch(error => {
      reject(error)
    })
  })
}

export const $put = (url, data) => {
  return new Promise((resolve, reject) => {
    instance({
      method: 'put',
      url: url,
      data: data
    }).then(res => {
      resolve(res)
    }).catch(error => {
      reject(error)
    })
  })
}

export const $patch = (url, data) => {
  return new Promise((resolve, reject) => {
    instance({
      method: 'patch',
      url: url,
      data: data
    }).then(res => {
      resolve(res)
    }).catch(error => {
      reject(error)
    })
  })
}
